package com.zretc.controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zretc.entity.Collect;
import com.zretc.entity.Comment;
import com.zretc.entity.Song;
import com.zretc.service.CollectService;
import com.zretc.service.CommentService;
import com.zretc.service.SongService;
import com.zretc.service.impl.CollectServiceImpl;
import com.zretc.service.impl.CommentServiceImpl;
import com.zretc.service.impl.SongServiceImpl;
import com.zretc.util.PageInfo;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/CommentServlet")
@MultipartConfig
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private CommentService commentService = new CommentServiceImpl();
    private SongService songService = new SongServiceImpl();
    private CollectService collectService = new CollectServiceImpl();
    Gson gson = new Gson();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("addComment".equals(op)) {
			doAddComment(request,response);
		}else if ("getCommentList".equals(op)) {
			doGetCommentList(request,response);
		}else if ("getSongDetail".equals(op)) {
			doGetSongDetail(request,response);
		}else if ("existCollect".equals(op)) {
			doExiseCollect(request,response);
		}else if ("addCollect".equals(op)) {
			doAddCollect(request,response);
		}else if ("deleteCollect".equals(op)) {
			doDeleteCollect(request,response);
		}
	}
	
	//删除收藏
	private void doDeleteCollect(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String accountId = request.getParameter("accountId");
		String songId = request.getParameter("songId");
		map.put("accountId", accountId);
		map.put("songId", songId);
		int addCollect = collectService.unCollect(map);
		if (addCollect>0) {
			response.getWriter().write("yes");
		}else {
			response.getWriter().write("no");
		}
	}

	//添加收藏
	private void doAddCollect(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String accountId = request.getParameter("accountId");
		String songId = request.getParameter("songId");
		map.put("accountId", accountId);
		map.put("songId", songId);
		int addCollect = collectService.addCollect(map);
		if (addCollect>0) {
			response.getWriter().write("yes");
		}else {
			response.getWriter().write("no");
		}
	}

	//当前歌曲是否被用户收藏了
	private void doExiseCollect(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String accountId = request.getParameter("accountId");
		String songId = request.getParameter("songId");
		Collect collect = collectService.getCollect(Integer.parseInt(songId), Integer.parseInt(accountId));
		if (collect==null) {
			response.getWriter().write("no");
		}else {
			response.getWriter().write("yes");
		}
	}

	//获取当前评论页面歌曲信息
	private void doGetSongDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String songId = request.getParameter("songId");
		Song songBySongId = songService.getSongBySongId(Integer.parseInt(songId));
		String json = gson.toJson(songBySongId);
		response.getWriter().write(json);
	}

	//获取该歌曲所有评论
	private void doGetCommentList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String songId = request.getParameter("songId");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		Map<String, String> map = new HashMap<String, String>();
		map.put("songId", songId);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Comment> commentBySongId = commentService.getCommentBySongId(map);
		String json = gson.toJson(commentBySongId);
		response.setContentType("application/json");
		response.getWriter().write(json);
	}
	
	//添加评论到数据库
	private void doAddComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String parameter = request.getParameter("comment");
		Comment fromJson = gson.fromJson(parameter,Comment.class);
		int addComment = commentService.addComment(fromJson);
		if (addComment>0) {
			response.getWriter().write("yes");
		}else {
			response.getWriter().write("no");
		}
	}

}
